#include "networkstreamer.h"
#include <QDebug>
#include <QNetworkInterface>

NetworkStreamer::NetworkStreamer(QObject *parent) : QObject(parent),
    streamingProcess(nullptr), isStreamingFlag(false)
{
    // 设置默认参数
    multicastAddress = "224.1.1.1";
    port = 5004;
    
    setupNetwork();
}

NetworkStreamer::~NetworkStreamer()
{
    if (isStreamingFlag) {
        stopStreaming();
    }
    
    if (streamingProcess) {
        streamingProcess->deleteLater();
    }
}

void NetworkStreamer::setMulticastAddress(const QString& address, int newPort)
{
    multicastAddress = address;
    port = newPort;
}

bool NetworkStreamer::initialize()
{
    qDebug() << "初始化网络推流器...";
    setupNetwork();
    qDebug() << "网络推流器初始化完成";
    return true;
}

bool NetworkStreamer::startStreaming()
{
    if (isStreamingFlag) {
        return false;
    }
    
    qDebug() << "开始网络推流到" << multicastAddress << ":" << port;
    
    // 设置推流状态
    isStreamingFlag = true;
    emit streamingStarted();
    
    qDebug() << "网络推流启动成功";
    return true;
}

void NetworkStreamer::stopStreaming()
{
    if (!isStreamingFlag) {
        return;
    }
    
    qDebug() << "停止网络推流...";
    
    isStreamingFlag = false;
    emit streamingStopped();
    
    qDebug() << "网络推流已停止";
}

bool NetworkStreamer::isStreaming() const
{
    return isStreamingFlag;
}

void NetworkStreamer::setupNetwork()
{
    streamingProcess = new QProcess(this);
    qDebug() << "网络推流器设置完成";
}
